﻿using Appliaction.Dto.Sinv;
using Appliaction.IAppServices.Sinv;
using AutoMapper;
using Domain.Entitys.Sinv;
using Domain.IRepositorys.Sinv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appliaction.AppServices.Sinv
{
    public class Sinv_Sale_ReturnService : ISinv_Sale_ReturnService
    {
        private readonly ISinv_Sale_ReturnRepository sinv_Sale_ReturnRepository;
        private readonly IMapper mapper;

        public Sinv_Sale_ReturnService(ISinv_Sale_ReturnRepository sinv_Sale_ReturnRepository,IMapper mapper)
        {
            this.sinv_Sale_ReturnRepository = sinv_Sale_ReturnRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 分页查询销售退货订单
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="customerName"></param>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public List<Sinv_Sale_Return_Info> GetSinv_Sale_Return_Infos(int currentPage, int pageSize, string customerName, string orderCode, int orderStatus, char auditStatus)
        {
            return sinv_Sale_ReturnRepository.GetSinv_Sale_Return_Infos(currentPage,pageSize,customerName,orderCode,orderStatus,auditStatus);
        }

        /// <summary>
        /// 添加销售退货信息及其详情
        /// </summary>
        /// <param name="saleReturnWithDetailsDto"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_ReturnAddAsync(SaleReturnWithDetailsDto saleReturnWithDetailsDto)
        {
            Sinv_Sale_Return sinv_Sale_Return = mapper.Map<Sinv_Sale_Return>(saleReturnWithDetailsDto.Sinv_Sale_ReturnAddDto);
            List<Sinv_Sale_Return_Details> sinv_Sale_Return_Details = mapper.Map<List<Sinv_Sale_Return_Details>>(saleReturnWithDetailsDto.Sinv_Sale_Return_DetailsAddDtos);
            return await sinv_Sale_ReturnRepository.Sinv_Sale_ReturnAddAsync(sinv_Sale_Return,sinv_Sale_Return_Details);
        }

        /// <summary>
        /// 修改订单审核状态、增加库存、修改销售明细退货数量、修改销售订单状态
        /// </summary>
        /// <param name="sinv_Sale_ReturnAuditUpdateDto"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_ReturnAuditUpdateAsync(Sinv_Sale_ReturnAuditUpdateDto sinv_Sale_ReturnAuditUpdateDto)
        {
            Sinv_Sale_Return sinv_Sale_Return = mapper.Map<Sinv_Sale_Return>(sinv_Sale_ReturnAuditUpdateDto);
            return await sinv_Sale_ReturnRepository.Sinv_Sale_ReturnAuditUpdateAsync(sinv_Sale_Return);
        }

        /// <summary>
        /// 根据销售订单退货查询对应销售退货信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<Sinv_Sale_Return> Sinv_Sale_ReturnByCodeAsync(string code)
        {
            return await sinv_Sale_ReturnRepository.Sinv_Sale_ReturnByCodeAsync(code);
        }

        /// <summary>
        /// 条件查询销售退货总数
        /// </summary>
        /// <param name="customerName"></param>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public Task<int> Sinv_Sale_ReturnCountAsync(string customerName, string orderCode, int orderStatus, char auditStatus)
        {
            return sinv_Sale_ReturnRepository.Sinv_Sale_ReturnCountAsync(customerName,orderCode,orderStatus,auditStatus);
        }

        /// <summary>
        /// 逻辑删除销售退货信息并修改销售明细预计退货数量
        /// </summary>
        /// <param name="sinv_Sale_ReturnDelDto"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_ReturnDelAsync(Sinv_Sale_ReturnDelDto sinv_Sale_ReturnDelDto)
        {
            Sinv_Sale_Return sinv_Sale_Return = mapper.Map<Sinv_Sale_Return>(sinv_Sale_ReturnDelDto);
            return await sinv_Sale_ReturnRepository.Sinv_Sale_ReturnDelAsync(sinv_Sale_Return);
        }

        /// <summary>
        /// 修改销售退货明细及其详情
        /// </summary>
        /// <param name="saleReturnWithDetailsUpdateDto"></param>
        /// <returns></returns>
        public async Task<int> Sinv_Sale_ReturnUpdateAsync(SaleReturnWithDetailsUpdateDto saleReturnWithDetailsUpdateDto)
        {
            Sinv_Sale_Return sinv_Sale_Return = mapper.Map<Sinv_Sale_Return>(saleReturnWithDetailsUpdateDto.Sinv_Sale_ReturnUpdateDto);
            return await sinv_Sale_ReturnRepository.Sinv_Sale_ReturnUpdateAsync(sinv_Sale_Return,saleReturnWithDetailsUpdateDto.Sinv_Sale_Return_Details);
        }

        /// <summary>
        /// 根据销售退货编号查询对应销售退货明细
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<List<Sinv_Sale_Return_Details>> Sinv_Sale_Return_DetailsByCodeAsync(string code)
        {
            return await sinv_Sale_ReturnRepository.Sinv_Sale_Return_DetailsByCodeAsync(code);
        }
    }
}
